package test;

import com.alibaba.fastjson.JSONObject;
import wiremock.org.apache.commons.codec.digest.DigestUtils;
import wiremock.org.apache.commons.lang3.StringUtils;

import java.util.Arrays;

public class SimpleJavaApp {

    public static void main(String[] args) throws InterruptedException {
        String signKey = "ddd560c4-c8e1-4083-beac-7841426ac3bf";
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("orderId","123");
        String sign = getSign(new JSONObject(), signKey);
        System.out.println(sign);
    }


    public static String getSign(JSONObject data, String signKey) {
        data.put("signKey", signKey);
        StringBuilder signParam = new StringBuilder();
        Object[] keys = data.keySet().toArray();
        Arrays.sort(keys);
        for (Object key : keys) {
            if ("sign".equals(key)) {
                continue;
            }
            Object valueObject = data.get(key);
            // 如果参数值为json对象的，不参与加密运算。
            if (valueObject != null && !valueObject.getClass().isAssignableFrom(JSONObject.class)) {
                String value = valueObject.toString();
                if (StringUtils.isNotBlank(value)) {
                    signParam.append(key).append(value);
                }
            }
        }
        String sign = DigestUtils.sha1Hex(signParam.toString());
        data.remove("signKey");
        return sign;
    }

}
